package com.test.learnopenglandroid;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import java.util.concurrent.ConcurrentHashMap;

public class UIHelper {

    public static void addTest(ViewGroup root, String name, View.OnClickListener testFun){
        Button btn =new Button(root.getContext());
        btn.setText(name);
        btn.setOnClickListener(testFun);
        btn.setTextSize(10.0f);
        root.addView(btn);
    }

    /**
     * 给测试界面添加一个用于调整数值的seekbar面板
     * 第一次出现是在 GLES20RendererLightingMapsChangeLight
     * */
    public static class SeekBarHelper{
        ConcurrentHashMap<String,Float> mSeekBarValue = new ConcurrentHashMap<>();
        LinearLayout rootView;

        /**
         * 数值会映射到 0 ～ mSeekBarMax 的区间
         * */
        private float mSeekBarMax = 100f;
        public SeekBarHelper(Context context,float seekBarMax){
            this.mSeekBarMax = seekBarMax;
            rootView = new LinearLayout(context);
            rootView.setOrientation(LinearLayout.VERTICAL);
        }

        public SeekBarHelper(Context context){
            rootView = new LinearLayout(context);
            rootView.setOrientation(LinearLayout.VERTICAL);
        }

        public float getValue(String key){
            return mSeekBarValue.get(key);
        }

        protected SeekBar.OnSeekBarChangeListener mSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                TextView tx1 = (TextView) seekBar.getTag();
                String name = (String) tx1.getTag();
                float value = ((float) i) / mSeekBarMax;
                mSeekBarValue.put(name, value);
                tx1.setText(name+ value);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        };
        public void addSeekBar(Context context,String name,float defValue){
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            TextView tx1 = new TextView(context);
            tx1.setText(name+defValue);
            tx1.setTag(name);
            SeekBar sb1= new SeekBar(context);
            sb1.setMax((int) mSeekBarMax);
            sb1.setTag(tx1);

            sb1.setProgress((int) (defValue*mSeekBarMax));
            mSeekBarValue.put(name,defValue);
            sb1.setOnSeekBarChangeListener(mSeekBarChangeListener);
            rootView.addView(tx1,lp);
            rootView.addView(sb1,lp);
        }
        public LinearLayout getSeekBarView(){
            return rootView;
        }
    }

    public interface CustomViewInterface{
        View getCustomView(Context context);
    }
}
